﻿using Azure.Core;
using B.S.Passport.Domain;
using B.S.Passport.ErrorCode;
using B.S.Passport.Infacetrue;
using B.S.Passport.Read.API.Application.Command123;
using B.S.Passport.Read.API.Dto;
using DAL;
using MediatR;
using System.Reflection.Metadata;

namespace B.S.Passport.Read.API.Application.Commandhander123
{
    public class GetWarehouseCommandHander : IRequestHandler<GetWarehouseCommand, ApiResult<Apipage<WhouaseDto>>>
    {
        public readonly IBaseRepository<WarehouseModel> WarebaseRepository;
        public readonly IBaseRepository<WHouseType> WHouseTypebaseRepository;
        public readonly IBaseRepository<WHouseModel> WHouseModelbaseRepository;
        public readonly DBHelper1 dBHelper;

        public GetWarehouseCommandHander(IBaseRepository<WarehouseModel> warebaseRepository, IBaseRepository<WHouseType> wHouseTypebaseRepository, IBaseRepository<WHouseModel> wHouseModelbaseRepository, DBHelper1 dBHelper)
        {
            WarebaseRepository = warebaseRepository;
            WHouseTypebaseRepository = wHouseTypebaseRepository;
            WHouseModelbaseRepository = wHouseModelbaseRepository;
            this.dBHelper = dBHelper;
        }
        /// <summary>
        /// 获取库位信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<Apipage<WhouaseDto>>> Handle(GetWarehouseCommand request, CancellationToken cancellationToken)
        {
            ApiResult<Apipage<WhouaseDto>> apiResult = new ApiResult<Apipage<WhouaseDto>>();
            apiResult.Code = (ApiEnums)Renum.显示成功;
            apiResult.Message = Renum.显示成功.ToString();

            try
            {
                string baseListSql = "SELECT count(*) FROM WHouseType A JOIN WarehouseModel B ON A.WHouseTypeId = B.WHouseTypeId JOIN WHouseModel C ON B.WHouseId = C.WHouseId WHERE B.isdelete = 0";
                string baseSql = "SELECT * FROM WHouseType A JOIN WarehouseModel B ON A.WHouseTypeId = B.WHouseTypeId JOIN WHouseModel C ON B.WHouseId = C.WHouseId WHERE B.isdelete = 0";

                List<string> conditions = new List<string>();

                // 新增 WHouseId 查询条件（int类型）
                if (request.WHouseId != null)
                {
                    conditions.Add($"B.WHouseId = {request.WHouseId}");
                }

                if (!string.IsNullOrEmpty(request.WarehouseName))
                {
                    conditions.Add($"B.WarehouseName LIKE '%{request.WarehouseName}%'");
                }
                if (!string.IsNullOrEmpty(request.WarehouseNumber))
                {
                    conditions.Add($"B.WarehouseNumber LIKE '%{request.WarehouseNumber}%'");
                }

                string conditionString = string.Join(" AND ", conditions);
                if (!string.IsNullOrEmpty(conditionString))
                {
                    baseListSql += " AND " + conditionString;
                    baseSql += " AND " + conditionString;
                }
                string list = baseListSql;
                string sql = $"{baseSql} ORDER BY B.WarehouseNumber OFFSET (({request.PageIndex} - 1) * {request.PageSize}) ROWS FETCH NEXT {request.PageSize} ROWS ONLY";

                Apipage<WhouaseDto> apipage = new Apipage<WhouaseDto>();
                apipage.TotalCount = (int)dBHelper.Scalar(list);
                apipage.PageCount = (int)Math.Ceiling(apipage.TotalCount * 1.0 / request.PageSize);
                apipage.List = dBHelper.GetList<WhouaseDto>(sql);
                apiResult.Data = apipage;
            }
            catch (Exception ex)
            {
                apiResult.Code = (ApiEnums)Renum.显示失败;
                apiResult.Message = $"查询数据时发生错误: {ex.Message}";
                apiResult.Data = new Apipage<WhouaseDto>();
            }

            return Task.FromResult(apiResult);
        }
    }
}
